//
//  BiUtil.m
//  XinBanEducation
//
//  Created by ChenBin on 14-5-3.
//  Copyright (c) 2014年 Nahan Cop. All rights reserved.
//

#import "BiUtil.h"
#import "BiFileManager.h"
#import<SystemConfiguration/SCNetworkReachability.h>
#import "sys/utsname.h"
#import <netinet/in.h>

@implementation BiUtil
+ (NSString *)getSendTmpFileName
{
    return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.jpg"];
}
+ (CGSize)calcTextSize:(NSString*)text textFont:(UIFont*)textFont width:(int)width
{
    if ([text isKindOfClass:[NSString class]])
    {
//         NSDictionary *dic = textFont.fontDescriptor.fontAttributes;
//        CGSize textSize = [text boundingRectWithSize:CGSizeMake(width, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
        CGSize textSize = [text sizeWithFont:textFont constrainedToSize:CGSizeMake(width, 1000) lineBreakMode:NSLineBreakByTruncatingTail];
        return textSize;
    }
    
    return CGSizeZero;
}
+ (BOOL)isBlankString:(NSString*)str
{
    if (![str isKindOfClass:[NSString class]])
    {
        return YES;
    }
    
    NSString *trimmedString = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (trimmedString.length == 0)
    {
        return YES;
    }
    
    return NO;
}
+ (NSString *)getDownloadImageUrl:(NSString*)imageSuffix
{
    return [NSString stringWithFormat:@"%@/%@", SERVICE_URL, imageSuffix];
}

+ (NSString *)getRandImageFileName
{  // 加一个随机数的目的是为了防止同一秒有多张图片
    int cur_time_interval = (int)[[NSDate date] timeIntervalSince1970];
    int rand = arc4random() % 100; // 获取 0-99之间的随机数
    return [NSString stringWithFormat:@"%d%d.png", cur_time_interval, rand];
}

+ (BOOL)isConnectToNetwork
{
    // Create zero addy
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;
	
    // Recover reachability flags
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;
	
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);
	
    if (!didRetrieveFlags)
    {
        NSLog(@"Error. Could not recover network reachability flags");
        return NO;
    }
	
    BOOL isReachable = flags & kSCNetworkFlagsReachable;
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
	BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
	
    // 向苹果网站发送请求，严正URL链接是否成功
    
	NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
	NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
	NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:self];
	
    return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
}
@end
